import psycopg2
import MySQLdb
import psycopg2.extras

class connectionDB:
	def __init__(self, configure):
		self.dbms = configure.get("dbms") 
		self.db_username = configure.get("db_username")
		self.db_password = configure.get("db_password")
		self.db_hostname = configure.get("db_hostname")
		self.db_name = configure.get("db_name")
		

	def connect(self):
		connection = None
		if self.dbms == "MySQL":
			connection = self.__conectToMySQL()
		elif self.dbms =="PostgreSQL":
			connection= self.__conectToPostgreSQL()
		elif self.dbms =="Oracle": 
			connection= self.__conectToOracle()
		return connection

	def __conectToMySQL(self):
		connection = MySQLdb.connect(host=self.db_hostname, user=self.db_username, passwd=self.db_password, db=self.db_name)
		return connection

	def __conectToPostgreSQL(self):
		connection = psycopg2.connect(database=self.db_name,user=self.db_username,password=self.db_password,host=self.db_hostname)
		return connection

	def __conectToOracle(self):
		pass

	def executeSelectQuery(self,query,conn):
		if self.dbms == "MySQL":
			return self.__executeSelectQueryMySQL(query,conn)
		elif self.dbms =="PostgreSQL":
			return self.__executeSelectQueryPostgreSQL(query,conn)

	def __executeSelectQueryPostgreSQL(self,query, conn):
		cursor = conn.cursor()
		cursor.execute(query)
		return cursor.fetchall()
		#for row in cursor:
			#yield row 	
		

	def __executeSelectQueryMySQL(self,query,conn):
		cursor = conn.cursor()
		cursor.execute(query)
		return cursor.fetchall()


	def executeQuery(self,query,conn):
		if self.dbms == "MySQL":
			return self.__executeQueryMySQL(query,conn)
		elif self.dbms =="PostgreSQL":
			return self.__executeQueryPostgreSQL(query,conn)

	def __executeQueryPostgreSQL(self,query, conn):
		cursor = conn.cursor()
		cursor.execute(query)
		conn.commit()
		cursor.close()
		#for row in cursor:
			#yield row 	

	def __executeQueryMySQL(self,query,conn):
		cursor = conn.cursor()
		cursor.execute(query)
		conn.commit()
		cursor.close()
